import { createSlice } from '@reduxjs/toolkit'

const initialState = {
  favorites: [],
  recentViews: [],
  filters: {
    cuisine: [],
    priceRange: [],
    distance: null
  }
}

export const foodSlice = createSlice({
  name: 'food',
  initialState,
  reducers: {
    toggleFavorite: (state, action) => {
      const foodId = action.payload
      const index = state.favorites.indexOf(foodId)
      if (index === -1) {
        state.favorites.push(foodId)
      } else {
        state.favorites.splice(index, 1)
      }
    },
    addRecentView: (state, action) => {
      const food = action.payload
      state.recentViews = [
        food,
        ...state.recentViews.filter(item => item.id !== food.id)
      ].slice(0, 10) // 只保留最近10条记录
    },
    updateFilters: (state, action) => {
      state.filters = {
        ...state.filters,
        ...action.payload
      }
    }
  }
})

export const { toggleFavorite, addRecentView, updateFilters } = foodSlice.actions

export default foodSlice.reducer 